/*
 * acooly.cn Inc.
 * Copyright (c) 2017 All Rights Reserved.
 * create by ouwen
 * date:2017-09-20
 */
package com.yiji.fintech.musfinance.service.impl;

import com.acooly.core.common.dao.support.PageInfo;
import com.acooly.core.common.service.EntityServiceImpl;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.yiji.fintech.musfinance.base.enums.CardTypeEnum;
import com.yiji.fintech.musfinance.base.enums.PersonalCorporateTypeEnum;
import com.yiji.fintech.musfinance.common.enums.StatusEnum;
import com.yiji.fintech.musfinance.common.exception.DBException;
import com.yiji.fintech.musfinance.dao.BusinessRuleDao;
import com.yiji.fintech.musfinance.entity.BusinessRule;
import com.yiji.fintech.musfinance.service.BusinessRuleService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * center_business_rule Service实现
 *
 * Date: 2017-09-20 23:38:05
 *
 * @author ouwen
 *
 */
@Service("businessRuleService")
public class BusinessRuleServiceImpl extends EntityServiceImpl<BusinessRule, BusinessRuleDao> implements BusinessRuleService {
    @Override
    public List<BusinessRule> query(BusinessRule businessRule, Map<String, Boolean> sortMap, int pageNum, int pageSize){
        Map<String, Object> map = Maps.newHashMap();
        map.put("EQ_partnerId", businessRule.getPartnerId());
        map.put("EQ_status", StatusEnum.ON);
        map.put("EQ_instBankId", businessRule.getInstBankId());
        map.put("EQ_transactionType", businessRule.getTransactionType());
        map.put("EQ_subTransactionType", businessRule.getSubTransactionType());
        if (businessRule.getPersonalCorporateType() != null) {
            if (businessRule.getPersonalCorporateType() == PersonalCorporateTypeEnum.ALL) {
                map.put("IN_personalCorporateType", PersonalCorporateTypeEnum.getAllEnum());
            } else {
                map.put("IN_personalCorporateType", Sets.newHashSet(businessRule.getPersonalCorporateType(), PersonalCorporateTypeEnum.ALL));
            }
        }
        if (businessRule.getCardType() != null) {
            if (businessRule.getCardType() == CardTypeEnum.ALL) {
                map.put("IN_cardType", CardTypeEnum.getAll());
            } else {
                map.put("IN_cardType", Sets.newHashSet(businessRule.getCardType(), CardTypeEnum.ALL));
            }
        }
        PageInfo<BusinessRule> pageInfo = new PageInfo<>();
        pageInfo.setCurrentPage(pageNum);
        pageInfo.setCountOfCurrentPage(pageSize);
        try {
            pageInfo = getEntityDao().query(pageInfo, map, sortMap);
        } catch (Exception e) {
            throw new DBException(e.getMessage());
        }
        return pageInfo.getPageResults();
    }
}
